#include "5obza.h"
#include <allegro.h>
#include <stdio.h>

#define GFX_W 800
#define GFX_H 600
#define NB_IMAGES 16




BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	return (Background);

}
BITMAP* load_background1_bmp(char chemin_background_col[])
{
	BITMAP* BackgroundCol; 
      BackgroundCol=load_bitmap(chemin_background_col, NULL);
      

	
	if(BackgroundCol == NULL  ){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	
      return (BackgroundCol);


}


void draw_backgroundcol(BITMAP *BackgroundCol,BITMAP*Buffer,float *b,int *pix3,int *pix1 )
{
if  (*b<18460){

	

blit(BackgroundCol,Buffer, *b, 0,0, 70,   SCREEN_W , SCREEN_H); 
if (*pix3!=255 && *pix1==170)
{*b=*b+2;}
}
}
 void load_backplat_bmp(plat *p)
{
	
	char buf[50];
	
		sprintf(buf, "../images/plat0.bmp");
		p->img_plat[0] = load_bitmap(buf, NULL);

}


void draw_plat( plat p ,BITMAP*Buffer ,float *a, int *pix3,int *pix1)
{


if  (*a>-18460 ){
draw_sprite(Buffer, p.img_plat[0],*a, 70);
if (*pix3!=255 && *pix1==170)
{*a=*a-2;}
}
}



void destroy_plat(plat *p)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++){
		destroy_bitmap(p->img_plat[i]);
                            }
}





void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}

void load_5obza(Personage *men)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "../images/right%d.bmp", i);
		men->img_right[i] = load_bitmap(buf, NULL);
		
	}
}

void destroy_5obza(Personage *men)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) {
		destroy_bitmap(men->img_right[i]);
		
	}

}
void init_5obza(Personage *men,int *compteur, float *a,float *c,float *b, int *d,int *f,int *e,int *g,int *h,int *k,int *l,int *m,int *n,int *o,int *r,int *s)
{
	men->direction = 0;
	men->x = 30 ;
	men->y = SCREEN_H-400 ;
*compteur = 0;
	
       * a=0; 
*c=0; *b=0; *d=0;*f=0; *h=0;*e=1;*g=2;*k=0; *l=2;*m=0;*n=0;*o=3; *r=2; *s=0;

}
void move_5obza_right(Personage *men,int *compteur,BITMAP * Background ,int *pix1)
{

 

	if ( *pix1==170) {
		*compteur+=1;

		//(men->x)+=0.5;
		men->direction = 0; //right
	}

}

/*void move_5obza_left(Personage *men,int *compteur,BITMAP * Background,int *pix2)
{

	if (key[KEY_LEFT] && *pix2!=0) {
		*compteur+=1;

		(men->x)-=1;
		men->direction = 1; 
	}
}
*/
void jump_5obza(Personage *men,BITMAP * Background,int *pix4,int *pix3,int *pix1)
{

	if (key[KEY_UP] && *pix3==0   ) {
		(men->y)-=280;
               
   //(men->x)+=0.5;
		men->direction = 2; 


}
	

}

void gravity_5obza(Personage *men,BITMAP * Background,int *pix3,int *pix1)
{ 


       if (   *pix3 != 0)
     {
          (men->y)+=3;
               men->direction = 3;

}
}




void move_5obza(Personage *men,int *compteur,BITMAP * Background,int *pix3,int *pix1,int *pix2,int *pix4)
{
	move_5obza_right(men,compteur,Background,pix1);
	//move_5obza_left(men,compteur,Background,pix2);
	jump_5obza(men,Background,pix4,pix3,pix1);
       gravity_5obza(men,Background,pix3,pix1);
	
}
void draw_5obza(Personage *men,BITMAP *Buffer, int *compteur)
{
	static int last_direction; 
	if (men->direction == 0){ 

		draw_sprite(Buffer, men->img_right[*compteur % (NB_IMAGES-2) ],men->x, men->y);
		last_direction=0;
	}

	
	if (men->direction == 2 && last_direction==0 ) {	
              

		draw_sprite(Buffer, men->img_right[NB_IMAGES - 2],men->x, men->y);
	}
	
        if (men->direction == 3 && last_direction==0) {	

		draw_sprite(Buffer, men->img_right[NB_IMAGES -1],men->x, men->y);
          }
      

}


 


int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}

void collision (Personage *men ,BITMAP *BackgroundCol, int *pix1,int *pix2,int *pix3,int *pix4,int *d)
{

if  (*d<18460){


            *pix1=get_pixel(BackgroundCol,men->x+ men->img_right[0]->w+*d ,men->y + men->img_right[0]->h /2-50);
           //*pix2=get_pixel(BackgroundCol,men->x+*d,men->y + men->img_left[0]->h /2);
           *pix3=get_pixel(BackgroundCol,men->x+ men->img_right[0]-> w/2+*d ,men->y + men->img_right[0]->h-70);
           *pix4=get_pixel(BackgroundCol,men->x+*d + men->img_right[3]->w /2,men->y-70);

if (*pix3!=255 && *pix1==170)
 {*d=*d+2;}
}
}
void jeux(BITMAP *BackgroundFin,BITMAP *BackgroundC1,BITMAP *BackgroundC2,BITMAP *BackgroundC3,BITMAP *BackgroundCol,BITMAP *Background,BITMAP *Buffer,plat p ,Personage *men ,int *pix1,int *pix2 ,int *pix3,int *pix4,float *a,float *b,int *d,int *o,int *compteur,int *h )
{

 //  charge(men,compteur,a,c,b,d,f,e,g,h,k,l,m,n,o,r,s);

   draw_backgroundcol(BackgroundCol,Buffer,b,pix3,pix1);
   draw_background(Background,Buffer);
 draw_plat(p,Buffer,a,pix3,pix1);

if(*o==3) {draw_sprite(Buffer,BackgroundC3,0,0);}
if(*o==2) {draw_sprite(Buffer,BackgroundC2,0,0);}
if(*o==1) {draw_sprite(Buffer,BackgroundC1,0,0);}

        collision (men,BackgroundCol,pix1,pix2,pix3,pix4,d);
  	
	
	
      move_5obza(men,compteur,BackgroundCol,pix3,pix1,pix2,pix4);
     draw_5obza(men,Buffer,compteur);
if (*pix1==85){draw_background(BackgroundFin,Buffer);
                     if(key[KEY_ENTER]){*h=1;}
  
                  }
 
    if (*compteur == 10000)
			*compteur = 0;


}

void acceuil(BITMAP *BackgroundAccueil,BITMAP *BackgroundWelc,BITMAP *Buffer,int *f)
{
while(!key[KEY_ENTER])

{
clear_bitmap(Buffer);
draw_background(BackgroundAccueil,Buffer);
if (*f<500){
draw_sprite(Buffer,BackgroundWelc,SCREEN_H-580,500-*f);

if (*f<499){*f=*f+1;}
}
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}
}
void f_pause(BITMAP *BackgroundPause,BITMAP *BackgroundCa,BITMAP *Buffer,int *m,int *k,int *l,int *h)
{

 draw_background(BackgroundPause,Buffer);

  
        if(key[KEY_DOWN]){*l=1;}
        if(key[KEY_UP]){*l=0;}
 
  if (*l==0 ||*l==2 ){draw_sprite(Buffer,BackgroundCa,SCREEN_H-600,0);
            if (key[KEY_ENTER]){*m=0;*k=0;}
            }
 if (*l==1){ draw_sprite(Buffer,BackgroundCa,SCREEN_H-600,60); 
              if (key[KEY_ENTER]){*h=1;}
           }



        }





void vie(BITMAP *BackgroundGameover,BITMAP *BackgroundChoix, BITMAP *BackgroundCader,BITMAP *BackgroundRe,BITMAP *BackgroundCv, BITMAP *BackgroundC1,BITMAP *BackgroundC2,BITMAP *BackgroundC3,BITMAP *Buffer,Personage *men,int *compteur, float *a,float *c,float *b, int *d,int *f,int *e,int *g,int *h,int *k,int *l,int *m,int *n,int *o,int *r,int *s)
{



if((*o==3)||(*o==2)){
draw_background(BackgroundRe,Buffer);
if(*o==3) {draw_sprite(Buffer,BackgroundC2,SCREEN_H/2,280);}
if(*o==2) {draw_sprite(Buffer,BackgroundC1,SCREEN_H/2,280);}
if(*g==2) {draw_sprite(Buffer,BackgroundCv,SCREEN_H-600,0);
if (key[KEY_ENTER]){*n=1;}
if (*n==1) {	men->direction = 0;
	men->x = 30 ;
	men->y = SCREEN_H-400 ;
*compteur = 0; *c=0; *f=0; *h=0;*e=1;*g=2;*k=0; *l=2;*m=0;*n=0;*r=2; *s=0;
   *o=*o-1;

}
}
if (key[KEY_DOWN]){*g=1;}
if (key[KEY_UP]){*g=0;}
if(*g==0) {draw_sprite(Buffer,BackgroundCv,SCREEN_H-600,0);
if (key[KEY_ENTER]){*n=1; }
if (*n==1) {	

  
men->direction = 0;
	men->x = 30 ;
	men->y = SCREEN_H-400 ;
*compteur = 0; *c=0; *f=0; *h=0;*e=1;*g=2;*k=0; *l=2;*m=0;*n=0;*r=2; *s=0; *o=*o-1;

}
}

if(*g==1){
draw_sprite(Buffer,BackgroundCv,SCREEN_H-600,60);
if (key[KEY_ENTER]){*h=1;}

}
}

if (*o==1){G_over(BackgroundGameover,BackgroundChoix,BackgroundCader,Buffer,men,compteur, a,c,b,d,f,e,g,h,k,l,m,n,o,r,s);}

}


void G_over(BITMAP *BackgroundGameover,BITMAP *BackgroundChoix, BITMAP *BackgroundCader,BITMAP *Buffer,Personage *men,int *compteur, float *a,float *c,float *b, int *d,int *f,int *e,int *g,int *h,int *k,int *l,int *m,int *n,int *o,int *r,int *s)
{




draw_background(BackgroundChoix,Buffer);

if(*r==2) {draw_sprite(Buffer,BackgroundCader,SCREEN_H-600,0);
if (key[KEY_P]){*s=1;}
if (*s==1) 
{	
   init_5obza(men,compteur,a,c,b,d,f,e,g,h,k,l,m,n,o,r,s);

}
}
if (key[KEY_DOWN]){*r=1;}
if (key[KEY_UP]){*r=0;}


if(*r==0 ) {draw_sprite(Buffer,BackgroundCader,SCREEN_H-600,0);
if (key[KEY_ENTER] ){*s=1;}
if (*s==1) 
{	
   init_5obza(men,compteur,a,c,b,d,f,e,g,h,k,l,m,n,o,r,s);

}
}

if(*r==1){
draw_sprite(Buffer,BackgroundCader,SCREEN_H-600,115);
if (key[KEY_ENTER]){*h=1;}

}


}

//}
/*void sauvgard(Personage *men,int *compteur, float *a,float *c,float *b, int *d,int *f,int *e,int *g,int *h,int *k,int *l,int *m,int *n,int *o,int *r,int *s)
{
FILE *fichier;
fichier=fopen ("tst.txt","w");
fprintf(fichier,"%d %f %f %d %f %f %f %d %d %d %d %d %d %d %d %d %d %d %d",men->direction,men->x,men->y,compteur,a,c,b,d,f,e,g,h,k,l,m,n,o,r,s);
fclose(fichier);

}

void charger(Personage *men,int *compteur, float *a,float *c,float *b, int *d,int *f,int *e,int *g,int *h,int *k,int *l,int *m,int *n,int *o,int *r,int *s)
{

FILE *fichier;
int entier[14] = {0} ; float reel[5] = {0};
fichier=fopen ("tst.txt","r");
fscanf(fichier,"%d %f %f %d %f %f %f %d %d %d %d %d %d %d %d %d %d %d %d",entier[0],reel[0],reel[1],entier[1],reel[2],reel[3],reel[4],entier[2],entier[3],entier[4],entier[5],entier[6],entier[7],entier[8],entier[9],entier[10],entier[11],entier[12],entier[13]);

men->direction=entier[0];
men->x=reel[0];
men->y=reel[1];
compteur=entier[1];a=reel[2];c=reel[3];b=reel[4];d=entier[2];f=entier[3];e=entier[4];g=entier[5];h=entier[6];k=entier[7];l=entier[8];m=entier[9];n=entier[10];o=entier[11];r=entier[12];s=entier[13];

fclose(fichier);
}
*/


